package com.cheng.commonutlis.avtivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.GsonUtils;
import com.cheng.commonlibrary.activity.CommonActivity;
import com.cheng.commonutlis.R;
import com.cheng.imagerelated.ImageFilePath;
import com.cheng.imagerelated.photoselecview.PhotoSelector;
import com.cheng.imagerelated.preview.PreViewSelector;
import com.cheng.imagerelated.uTakePhoto.compress.CompressConfig;
import com.cheng.imagerelated.uTakePhoto.crop.CropOptions;
import com.cheng.imagerelated.uTakePhoto.exception.TakeException;
import com.cheng.imagerelated.uTakePhoto.manager.ITakePhotoResult;
import com.cheng.imagerelated.uTakePhoto.manager.UTakePhoto;
import com.cheng.imagerelated.uTakePhoto.utils.TUriUtils;

import java.util.ArrayList;
import java.util.List;

public class TakePhotoActivity extends CommonActivity {

    private ImageView imageView;

    @Override
    protected int loadLayoutResId() {
        return R.layout.activity_take_photo;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageView = findViewById(R.id.my_image);

    }

    public void photo(View view) {
        // 拍照


        UTakePhoto.with(this).openCamera(TUriUtils.getPhotoAlbum(this)).build(new ITakePhotoResult() {
            @Override
            public void takeSuccess(List<Uri> uriList) {
                for (int i = 0; i < uriList.size(); i++) {
                    imageView.setImageURI(uriList.get(i));
                }
            }

            @Override
            public void takeFailure(TakeException ex) {

            }

            @Override
            public void takeCancel() {

            }
        });
    }

    public void select_iamge(View view) {
        // 选择图片
        UTakePhoto.with(this).openAlbum().build(new ITakePhotoResult() {
            @Override
            public void takeSuccess(List<Uri> uriList) {
                for (int i = 0; i < uriList.size(); i++) {
                    imageUri = uriList.get(i);
                }
            }

            @Override
            public void takeFailure(TakeException ex) {

            }

            @Override
            public void takeCancel() {

            }
        });
    }

    public void tailor_iamge(View view) {
        // 裁剪
        CropOptions cropOptions = new CropOptions.Builder()
                .setAspectX(1).setAspectY(1)
                .setOutputX(100).setOutputY(1)
                .setWithOwnCrop(true)//使用系统裁剪还是自带裁剪
                .create();
        UTakePhoto.with(this).openAlbum().setCrop(cropOptions).build(new ITakePhotoResult() {
            @Override
            public void takeSuccess(List<Uri> uriList) {

            }

            @Override
            public void takeFailure(TakeException ex) {

            }

            @Override
            public void takeCancel() {

            }
        });
    }

    private Uri imageUri;

    public void compress_iamge(View view) {
        // 图片压缩
        CompressConfig compressConfig = new CompressConfig.Builder()
                .setFocusAlpha(false)//是否支持透明度
                .setLeastCompressSize(200)//最小压缩尺寸
                .setTargetUri(imageUri)//压缩图片储存路径
                .create();
        UTakePhoto.with(this).openAlbum().setCompressConfig(compressConfig).build(new ITakePhotoResult() {
            @Override
            public void takeSuccess(List<Uri> uriList) {
                for (int i = 0; i < uriList.size(); i++) {
                    imageView.setImageURI(uriList.get(i));
                }
            }

            @Override
            public void takeFailure(TakeException ex) {

            }

            @Override
            public void takeCancel() {

            }
        });


    }


    public static final int SELECTED_IMAGE = 1950;

    public void select_iamges(View view) {
        ArrayList<String> images = new ArrayList<>();
//        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/b22397bed4e92e3cd442ca2f85d08f2c.jpg");
//        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/87877cad4b45267ec726f505624db7d7.jpg");
//        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/13809373f6840210cc20a9386f8d534d.png");
        images.add("content://media/external/images/media/6306");
        // 选择多图
        PhotoSelector.builder()
                .setShowCamera(true)//显示拍照
                .setGridColumnCount(3)//列数
                .setMaxSelectCount(4)
                .setSelected(images)
                .setMaterialDesign(true)//design风格
                .setToolBarColor(ContextCompat.getColor(this, R.color.white))//toolbar的颜色
                .setBottomBarColor(ContextCompat.getColor(this, R.color.colorBlue))//底部bottombar的颜色
                .setStatusBarColor(ContextCompat.getColor(this, R.color.colorBlue))//状态栏的颜色
                .setStatusTextBarColor(true)        // 状态栏字体是否为黑色
                .start(this, SELECTED_IMAGE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case SELECTED_IMAGE:
                    ArrayList<String> stringArrayListExtra = data.getStringArrayListExtra(PhotoSelector.SELECT_RESULT);
                    if (stringArrayListExtra.isEmpty()) {
                        return;
                    }
                    Log.d(TAG, "onActivityResult: " + GsonUtils.toJson(stringArrayListExtra));
                    break;
            }
        }
    }

    private static final String TAG = "TakePhotoActivity";

    public void webview(View view) {
//        startActivity(new Intent(getActivity(), WebViewActivity.class));
        Uri parse = Uri.parse("content://media/external/images/media/6306");
        ImageFilePath.getFilePathFromURI(this, parse);
    }

    public void select_preview(View view) {
        // 图片预览
        ArrayList<String> images = new ArrayList<>();
        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/b22397bed4e92e3cd442ca2f85d08f2c.jpg");
        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/87877cad4b45267ec726f505624db7d7.jpg");
        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/13809373f6840210cc20a9386f8d534d.png");
        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/b22397bed4e92e3cd442ca2f85d08f2c.jpg");
        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/87877cad4b45267ec726f505624db7d7.jpg");
        images.add("https://cqjiaguwen.oss-cn-chengdu.aliyuncs.com/uploads/20200622/13809373f6840210cc20a9386f8d534d.png");

        PreViewSelector.builder()
                .setSelected(images)
                .setToolBarColor(ContextCompat.getColor(this, R.color.white))//toolbar的颜色
                .setBottomBarColor(ContextCompat.getColor(this, R.color.colorBlue))//底部bottombar的颜色
                .setStatusBarColor(ContextCompat.getColor(this, R.color.colorBlue))//状态栏的颜色
                .setStatusTextBarColor(true)        // 状态栏字体是否为黑色
                .start(this, SELECTED_IMAGE);
    }
}
